﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using zichan.BLL;
using zichan.Entity;
using zichan.Common;

namespace zichan.Controllers
{
    public class RepertoryManageController : BaseController
    {
        // GET: RepertoryManage
        /// <summary>
        /// 
        /// </summary>
        /// <param name="code">编码</param>
        /// <param name="userName">使用者名称</param>
        /// <param name="name">资产名称</param>
        /// <param name="remarkKeyword">备注关键字</param>
        /// <param name="adminName">管理员名称</param>
        /// <param name="assetClassId">资产类别</param>
        /// <param name="companyId">公司</param>
        /// <param name="statu">状态</param>
        /// <param name="sdt">开始时间</param>
        /// <param name="edt">结束时间</param>
        /// <param name="page">页数</param>
        /// <returns></returns>
        public ActionResult Index(string code, string userName, string name, string remarkKeyword, string adminName, int? assetClassId,int? serverPurposeId, int? companyId, int? statu, DateTime? sdt, DateTime? edt, int? page, int? pageSize, int? pageCount, int? selectedPage)
        {
            page = page ?? 1;
            pageSize = pageSize ?? 10;
            if (selectedPage.HasValue)
                page = selectedPage;
            if (pageCount.HasValue)
                page = page <= pageCount ? page : pageCount;
            if (page < 1) page = 1;
            ViewBag.page = page;
            ViewBag.pageSize = pageSize;
            ViewBag.selectedPage = selectedPage;
            List<SelectListItem> items = new List<SelectListItem> { new SelectListItem { Value = "10", Text = "10" }, new SelectListItem { Value = "20", Text = "20" }, new SelectListItem { Value = "50", Text = "50" }, new SelectListItem { Value = "100", Text = "100" } };
            ViewBag.pageSizes = new SelectList(items, "Value", "Text", ViewBag.pageIndex);

            ViewBag.code = code;
            ViewBag.userName = userName;
            ViewBag.name = name;
            ViewBag.remarkKeyword = remarkKeyword;
            ViewBag.adminName = adminName;
            ViewBag.assetClassId = assetClassId;
            ViewBag.companyId = companyId;
            ViewBag.statu = statu;
            ViewBag.sdt = sdt;
            ViewBag.serverPurposeId = serverPurposeId;
            if (sdt.HasValue)
                ViewBag.sdt = sdt.Value.ToShortDateString();
            ViewBag.edt = edt;
            if (edt.HasValue)
                ViewBag.edt = edt.Value.ToShortDateString();
           
            ViewBag.assetClassIds = new SelectList(AsseteClassService.GetAsseteClassList(string.Empty, string.Empty), "Id", "Name", assetClassId);
            ViewBag.companyIds = new SelectList(CompanyClassService.GetCompanyList(), "Id", "Name", companyId);
            List<SelectListItem> statuItems = new List<SelectListItem> { new SelectListItem { Value = "1", Text = "库存" }, new SelectListItem { Value = "2", Text = "使用中" }, new SelectListItem { Value = "3", Text = "报废" } };
            ViewBag.status = new SelectList(statuItems, "Value", "Text", ViewBag.statu);

            return View(RepertoryService.List(code, userName, name, remarkKeyword, adminName, assetClassId, serverPurposeId, companyId, statu, sdt, edt, page.Value, pageSize.Value));
        }
        public ActionResult Details(int id)
        {
            zichan.Entity.Asset model = RepertoryService.Get(id);
            return View(model);
        }
        public ActionResult part_Details(int id)
        {
            List<ConsumateInfo> list = new List<ConsumateInfo>();
            list = ConsumateInfoService.GetList(id);
            return PartialView(list);
        }
        [HttpPost]
        public ActionResult part_Details(List<string> FieldContent, List<int> FieldId, List<string> FieldName, List<int> AssetId, List<int> Id)
        {
            List<ConsumateInfo> list = new List<ConsumateInfo>();
            for (int i = 0; FieldContent.Count > i; i++)
            {
                ConsumateInfo model = new ConsumateInfo();
                model.FieldContent = FieldContent[i];
                model.FieldId = FieldId[i];
                model.FieldName = FieldName[i];
                model.AssetId = AssetId[i];
                model.Id = Id[i];
                list.Add(model);

            }
            string err = ConsumateInfoService.EditFieldList(list);
            if (string.IsNullOrEmpty(err))
            {
                TempData["alert"] = "success#信息已修改";
                UsageLogService.AddBaseToId(AssetId[0]);
            }
            else
            {
                TempData["alert"] = "error#" + err;
            }
            return RedirectToAction("Details", new { id = AssetId[0] });
        }
        public ActionResult partServer_Details(int id)
        {
            ServerInfo model = ServerInfoService.Get(id);
            ViewBag.Staffs = new SelectList(StaffService.StaffAll(1), "ID", "Name", model.AdminiStratorId);
            ViewBag.id = id;
            return PartialView(model);
        }
        [HttpPost]
        public ActionResult partServer_Details(ServerInfo model)
        {
            if (!ModelState.IsValid)
            {
                TempData["alert"] = "error#请重新填写数据，带*为必填";
                return RedirectToAction("Details", new { id = model.AssetId });
            }

            string err = ServerInfoService.Edit(model);
            if (string.IsNullOrEmpty(err))
            {
                TempData["alert"] = "success#信息已修改";
                UsageLogService.AddBaseToId(model.AssetId);
            }
            else
            {
                TempData["alert"] = "error#无法修改";
            }
            return RedirectToAction("Details", new { id = model.AssetId });
        }
        /// <summary>
        /// 入库
        /// </summary>
        /// <returns></returns>
        public ActionResult PutWareHouse(int id)
        {
            zichan.Models.PutAndScrapModel model = new Models.PutAndScrapModel();
            model.AssetId = id;
            model.AssetClassId= AssetService.Get(id).AssetClassId;
            
            return View(model);
        }
        [HttpPost]
        public ActionResult PutWareHouse(zichan.Models.PutAndScrapModel model)
        {
            zichan.Entity.Asset asset = AssetService.Get(model.AssetId);
            asset.RecoveryRemark = model.Remark;
            asset.UserId = null;
            asset.Status = (int)AsseteStatu.Repertory;
            asset.StatusTime = DateTime.Now;
            string err = AssetService.Edit(asset);
            if(string.IsNullOrEmpty(err))
            {
                TempData["alert"] = "success#入库成功";
                UsageLogService.AddBaseToId(model.AssetId, "回收入库", model.Remark,asset.Recipient);
                AssetLogService.AddLog("入库 备注：" + model.Remark, model.AssetId, asset.Recipient);
            }
            else
            {
                TempData["alert"] = "error#"+err;
            }
            return RedirectToAction("Index");
        }
        /// <summary>
        /// 出库
        /// </summary>
        /// <returns></returns>
        public ActionResult OutWareHouse(int id)
        {
            zichan.Models.OutModel model = new Models.OutModel();
            model.AssetClassId= AssetService.Get(id).AssetClassId;
            model.AssetId = id;
            ViewBag.userIds = new SelectList(StaffService.StaffAll(1), "Id", "Name");
            //ViewBag.serverPurposeIds = new SelectList(ServerPurposeService.GetList(), "serverPurposeId", "serverPurposeName");
            //ViewBag.serverPurposeSubIds = new SelectList(ServerPurposeSubClassService.GetList(), "serverPurposeSubId", "serverPurposeSubName");
            return View(model);
        }
        [HttpPost]
        public ActionResult OutWareHouse(zichan.Models.OutModel model)
        {
            if(!ModelState.IsValid)
            {
                TempData["alert"] = "error#请重新填写数据，带*为必填";
                return RedirectToAction("Index");
            }
            zichan.Entity.Asset asset = AssetService.Get(model.AssetId);
            asset.DistributionRemark = model.Remark;
            asset.UserId = model.userId;
            //asset.serverPurposeId = model.serverPurposeId;
            //asset.serverPurposeSubId = model.serverPurposeSubId;
            asset.Status = (int)AsseteStatu.InUse;
            asset.StatusTime = DateTime.Now;
            string err = AssetService.Edit(asset);
            if (string.IsNullOrEmpty(err))
            {
                TempData["alert"] = "success#出库成功";
                UsageLogService.AddBaseToId(model.AssetId, "出库", model.Remark, model.userId);
                AssetLogService.AddLog("出库 备注：" + model.Remark, model.AssetId, model.userId);
            }
            else
            {
                TempData["alert"] = "error#出库失败";
            }
            return RedirectToAction("Index");
        }
        /// <summary>
        /// 报废
        /// </summary>
        /// <returns></returns>
        public ActionResult Scrap(int id)
        {
            zichan.Models.PutAndScrapModel model = new Models.PutAndScrapModel();
            model.AssetId = id;
            return View(model);
        }
        [HttpPost]
        public ActionResult Scrap(zichan.Models.PutAndScrapModel model)
        {
            zichan.Entity.Asset asset = AssetService.Get(model.AssetId);
            asset.ScrapRemark = model.Remark;
            asset.Status = (int)AsseteStatu.Scrap;
            asset.StatusTime = DateTime.Now;
            ViewBag.layerClose = 1;
            string err = AssetService.Edit(asset);
            if (string.IsNullOrEmpty(err))
            {
                TempData["alert"] = "success#报废成功";
                UsageLogService.AddBaseToId(model.AssetId, "报废", model.Remark,null);
                AssetLogService.AddLog("报废 备注：" + model.Remark, model.AssetId, asset.Recipient);
            }
            else
            {
                TempData["alert"] = "error#报废失败";
            }
            return View(model);
            //return RedirectToAction("Index");
        }
    }
}